package ch.justdoit;

import java.util.ArrayList;

import ch.justdoit.andriodrpncalc.model.Model;
import ch.justdoit.andriodrpncalc.model.Settings;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.LinearLayout.LayoutParams;

public class SettingsView extends Activity {

	private Model model;
	private Spinner spinnPoints;
	private Spinner spinnType;
	private TextView debug;
	private Spinner spinnRounding;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		model = Model.getInstance();
		//setTheme(android.R.style.);
		LinearLayout linLayout = new LinearLayout(this);
		linLayout.setOrientation(LinearLayout.VERTICAL);
		setContentView(linLayout);

		TextView tv = new TextView(this);
		tv.setText("Digits after Point");
		linLayout.addView(tv, new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		spinnPoints = new Spinner(this);
		ArrayList<String> items = new ArrayList<String>();
		items.add("0");
		items.add("1");
		items.add("2");
		items.add("3");
		items.add("4");
		items.add("5");
		items.add("6");
		ArrayAdapter<String> digits = new ArrayAdapter<String>(this,
				R.layout.stack_list_item, items);
		spinnPoints.setAdapter(digits);
		spinnPoints.setSelection(model.mSettings.getDigitsAfterPoint());
		linLayout.addView(spinnPoints, new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

		TextView tvRounding = new TextView(this);
		tvRounding.setText("Rounding");
		linLayout.addView(tvRounding, new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		spinnRounding = new Spinner(this);
		items = new ArrayList<String>();
		items.add("none");
		items.add("4/5");
		items.add("5/6");
		items.add("floor");
		items.add("ciel");
		ArrayAdapter<String> roundings = new ArrayAdapter<String>(this,
				R.layout.stack_list_item, items);
		spinnRounding.setAdapter(roundings);

		if (model.mSettings.getRounding() == Settings.ROUNDING.NONE) {
			spinnRounding.setSelection(0);
		}
		if (model.mSettings.getRounding() == Settings.ROUNDING.R45) {
			spinnRounding.setSelection(1);
		}
		if (model.mSettings.getRounding() == Settings.ROUNDING.R56) {
			spinnRounding.setSelection(2);
		}
		if (model.mSettings.getRounding() == Settings.ROUNDING.FLOOR) {
			spinnRounding.setSelection(3);
		}
		if (model.mSettings.getRounding() == Settings.ROUNDING.CIEL) {
			spinnRounding.setSelection(4);
		}
		linLayout.addView(spinnRounding, new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

		TextView tv2 = new TextView(this);
		tv2.setText("Number Type");
		linLayout.addView(tv2, new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		spinnType = new Spinner(this);
		items = new ArrayList<String>();
		items.add("REALS");
		items.add("BIGREALS");
		items.add("INTEGER");
		items.add("RATIONALS");

		ArrayAdapter<String> types = new ArrayAdapter<String>(this,
				R.layout.stack_list_item, items);
		spinnType.setAdapter(types);

		if (model.mSettings.getCalcType() == Settings.TYPE.REALS) {
			spinnType.setSelection(0);
		}
		if (model.mSettings.getCalcType() == Settings.TYPE.BIGREALS) {
			spinnType.setSelection(1);
		}
		if (model.mSettings.getCalcType() == Settings.TYPE.INTEGER) {
			spinnType.setSelection(2);
		}
		if (model.mSettings.getCalcType() == Settings.TYPE.RATIONALS) {
			spinnType.setSelection(3);
		}
		linLayout.addView(spinnType, new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

		Button btnSave = new Button(this);
		btnSave.setText("Save Settings");

		btnSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// save digits
				Integer i = spinnPoints.getSelectedItemPosition();
				debug.append("digit:" + i.toString());
				model.mSettings.setDigitsAfterPoint(spinnPoints
						.getSelectedItemPosition());

				// save Type
				if (spinnType.getSelectedItem().toString() == "REALS") {
					model.mSettings.setCalcType(Settings.TYPE.REALS);
				}
				if (spinnType.getSelectedItem().toString() == "BIGREALS") {
					model.mSettings.setCalcType(Settings.TYPE.BIGREALS);
				}
				if (spinnType.getSelectedItem().toString() == "INTEGER") {
					model.mSettings.setCalcType(Settings.TYPE.INTEGER);
				}
				if (spinnType.getSelectedItem().toString() == "RATIONALS") {
					model.mSettings.setCalcType(Settings.TYPE.RATIONALS);
				}
				
				
				// save Rounding

				if (spinnRounding.getSelectedItem().toString() == "none") {
					model.mSettings.setRounding(Settings.ROUNDING.NONE);
				}
				if (spinnRounding.getSelectedItem().toString() == "4/5") {
					model.mSettings.setRounding(Settings.ROUNDING.R45);
				}
				if (spinnRounding.getSelectedItem().toString() == "5/6") {
					model.mSettings.setRounding(Settings.ROUNDING.R56);
				}
				if (spinnRounding.getSelectedItem().toString() == "floor") {
					model.mSettings.setRounding(Settings.ROUNDING.FLOOR);
				}
				if (spinnRounding.getSelectedItem().toString() == "ciel") {
					model.mSettings.setRounding(Settings.ROUNDING.CIEL);
				}
			}

		});

		linLayout.addView(btnSave, new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

		debug = new TextView(this);
		linLayout.addView(debug, new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		debug.setVisibility(Button.INVISIBLE);

	}

}
